package cn.mesmile.code.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author zb
 * @date 2021/5/6 18:10
 * @Description   已验证： 集群情况下 --》 springboot 自带的监听器，不会重复消费；
 *                 哪一个端口发送的消息，就只会被那个端口消费
 */
@Component
@Slf4j
public class ErrorLogListener {

    /**
     * 事件发布者
     * 事件监听者
     * 事件本身
     */

    @Async
    @Order
    @EventListener(ErrorLogEvent.class)
    public void saveErrorLog(ErrorLogEvent errorLogEvent){
        System.out.println("-------------------------------------");
        System.out.println("-----------监听者接收到消息-----------");
        System.out.println("-------------------------------------");
        Map<String,Object> source = (Map<String, Object>) errorLogEvent.getSource();
        System.out.println("source.toString() = " + source.toString());
    }

    /*
     * @Component
     * public class EmailListener implements ApplicationListener<EmailEvent> {
     *     @Override
     *     public void onApplicationEvent(EmailEvent emailEvent) {
     *         Object source = emailEvent.getSource();
     *            邮件监听器，一旦有邮件事件，则会触发此处,接收到消息
     *      System.out.println("邮件地址："+emailEvent.getEmail());
     *      System.out.println("邮件内容："+emailEvent.getContent());
     *  }
     */


}
